#include "bbh_logger_utils.h"

#include "bbh_include.h"

static int test_file_expire(const char*s, const char*extname, time_t cur, int interval, int keep_count)
{
    int idx = 0, n;
    struct tm m;

    //const char* e = s + strlen(s);
    const char* sp = strptime(s, "_%Y%m%dT%H%M%S", &m);
    if (sp - s != 16) {
        return 0;
    }
    if (*sp == '(') {
        if (1 != sscanf(sp, "(%d)%n", &idx, &n)) {
            return 0;
        }
        sp += n;
    }
    if (*sp != '.') {
        return 0;
    }
    sp++;
    if (strcmp(sp, extname)) {
        return 0;
    }

    // time
    time_t lt = mktime(&m);
    for (int i = 0; i < keep_count; i++) {
        if (cur - interval * i == lt) {
            return 0;
        }
    }

    return 1;
}

int bbh_logger_rolling_file(const char*file_dir, const char*file_name, const char*file_extname, time_t cur, int interval, int keep_count)
{
    DIR *dir;
    struct dirent *ptr;
    char file_path[PATH_MAX];//FILE_MAX_LEN

    size_t filename_len;
    //size_t extname_len;

    int r = 0;

    if ((dir = opendir(file_dir)) == NULL)
    {
        return -1;
    }
    filename_len = strlen(file_name);

    /*
    * 文件(8)、目录(4)、链接文件(10)
    */

    while ((ptr = readdir(dir)) != NULL)
    {
        if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
            continue;
        else if (ptr->d_type == 8) {
            if (strncmp(ptr->d_name, file_name, filename_len) == 0) {
                if (test_file_expire(ptr->d_name + filename_len, file_extname, cur, interval, keep_count)) {
                    sprintf(file_path, "%s/%s", file_dir, ptr->d_name);
                    unlink(file_path);
                    r++;
                }
            }
            //files->push_back(path + ptr->d_name);
        } else if (ptr->d_type == 10)
            continue;
        else if (ptr->d_type == 4)
        {
            //out->push_back(ptr->d_name);
            //getFiles(path + ptr->d_name + "/", files);
        }
    }
    closedir(dir);
    return r;
}

static int test_file_idx(const char*s, const char*extname, time_t cur, int interval)
{
    int idx = 0, n;
    struct tm m;

    //const char* e = s + strlen(s);
    const char* sp = strptime(s, "_%Y%m%dT%H%M%S", &m);
    if (sp - s != 16) {
        return 0;
    }
    // time
    if (cur != mktime(&m)) {
        return 0;
    }

    if (*sp == '(') {
        if (1 != sscanf(sp, "(%d)%n", &idx, &n)) {
            return 0;
        }
        sp += n;
    }
    if (*sp != '.') {
        return 0;
    }
    sp++;
    if (strcmp(sp, extname)) {
        return 0;
    }

    return idx;
}

int bbh_logger_rolling_idx(const char*file_dir, const char*file_name, const char*file_extname, time_t cur, int interval, int keep_count)
{
    DIR *dir;
    struct dirent *ptr;
    size_t filename_len;

    int r = 0, idx;

    if ((dir = opendir(file_dir)) == NULL)
    {
        return -1;
    }
    filename_len = strlen(file_name);
    /*
    * 文件(8)、目录(4)、链接文件(10)
    */
    while ((ptr = readdir(dir)) != NULL)
    {
        if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
            continue;
        else if (ptr->d_type == 8) {
            if (strncmp(ptr->d_name, file_name, filename_len) == 0) {
                idx = test_file_idx(ptr->d_name + filename_len, file_extname, cur, interval);
                if (r < idx) {
                    r = idx;
                }
            }
        } else if (ptr->d_type == 10)
            continue;
        else if (ptr->d_type == 4)
        {
        }
    }
    closedir(dir);
    return r;
}
